package com.yuye.excel.base.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.excel.util.StringUtils;
import com.yuye.excel.base.dto.ExcelLanguageDTO;
import com.yuye.excel.easyexcel.handle.EasyExcelAspect;
import com.yuye.excel.easyexcel.handle.vali.ExcelCheckManager;
import com.yuye.excel.enums.ExcelLanguageConstant;

/**
 * 
 * 为了 解决 多语言转换
 *
 * @author xgf
 * @version C10 2021年1月20日
 * @since SDP V300R003C10
 */
@Component
public class LanguageTransformationHandler {
	//公共的 语言转换
	private static final List<ExcelLanguageDTO> commonLanguageTraList = new ArrayList<>();

	// 自定义的 语言转换 是一个接口 
	// map key是 包名+ class名    |  value 为 自定义的语种转换
	// 定义1个接口类 2个方法  分别是公共的 和 特殊的
	private static final Map<String, List<ExcelLanguageDTO>> specialLanguageTraMap = new HashMap<>();
	
	public static final Map<String, List<ExcelLanguageDTO>> realHandelerMap = new HashMap<>();
	
	private static final String BASE_KEY = "base_key";
	/**
	 * 语言是 通过一个参数 决定的 excelLanguage
	 * 
	 * 语言的具体转换是 可以自定义的
	 *     key是 中文 value 是多语言的显示方式
	 *     
	 *     多语言的显示方式 分为 可以新起一个接口 方法 参数是 joint 然后 返回值 为 什么 语言
	 */

	static {
		//基础数据 赋值
		
	}
	@Autowired(required = false)
	LanguageTransformationInterface languageTransformationInterface;
	
	@PostConstruct
	private void init() {
		if(languageTransformationInterface == null) {
			return;
		}
		languageTransformationInterface.commonLanguageTra();
		languageTransformationInterface.specialLanguageTra();
		List<ExcelLanguageDTO> commonLanguageTra2 = languageTransformationInterface.commonLanguageTra;
		if(commonLanguageTra2 != null && commonLanguageTra2.size() > 0) {
			commonLanguageTraList.addAll(commonLanguageTra2);
		}
		
		Map<String, List<ExcelLanguageDTO>> specialLanguageTra2 = languageTransformationInterface.specialLanguageTra;
		if(specialLanguageTra2 != null) {
			specialLanguageTraMap.putAll(specialLanguageTra2);
		}
		Set<String> keySet = specialLanguageTra2.keySet();
		for (String string : keySet) {
			realHandelerMap.put(string, new ArrayList<>());
		}
		Iterator<Entry<String, List<ExcelLanguageDTO>>> iterator = realHandelerMap.entrySet().iterator();
		Entry<String, List<ExcelLanguageDTO>> next = null;
		//公共的值 赋值
		while(iterator.hasNext()) {
			next = iterator.next();
			String key = next.getKey();
			List<ExcelLanguageDTO> arrayList = new ArrayList<>();
			arrayList.addAll(commonLanguageTraList);
			arrayList.addAll(specialLanguageTraMap.get(key));
			realHandelerMap.put(key, arrayList);
		}
		
		//特殊值赋值
		realHandelerMap.put(BASE_KEY, commonLanguageTraList);
	}
	
	public static void main(String[] args) {
		
		ExcelLanguageDTO excelLanguageDTO = new ExcelLanguageDTO("名称", "name", ExcelLanguageConstant.ENGLISH);
		
		commonLanguageTraList.add(excelLanguageDTO);
		ExcelLanguageDTO excelLanguageDTO2 = new ExcelLanguageDTO("年龄", "age", ExcelLanguageConstant.ENGLISH);
		commonLanguageTraList.add(excelLanguageDTO2);
		
		
		
	}

	/**
	 * 
	 * 语种转换 
	 *
	 * @author xgf
	 * @param excelLanguageStatus 需要转换为 的 语种状态
	 */
	public static String transformation(ExcelCheckManager excelCheckManager, List<List<String>> errMsgList) {
		
		String excelLanguageStatus = excelCheckManager.excelLanguage(null);
		if(StringUtils.isEmpty(excelLanguageStatus) || ExcelLanguageConstant.CHINA.equals(excelLanguageStatus)) {
			return toString(errMsgList);
		}
		
		Map<String, List<ExcelLanguageDTO>> groupByChinaAndStatusMap = getMapByClassName(errMsgList);
		
		return toStringForList(errMsgList, excelLanguageStatus, groupByChinaAndStatusMap);
		
	}
	
	private static String toStringForList(List<List<String>> errMsgList, String excelLanguageStatus, Map<String, List<ExcelLanguageDTO>> groupByChinaAndStatusMap) {
		 StringBuilder sb = new StringBuilder();
		 for (List<String> errMsg : errMsgList) {
			 for (String content : errMsg) {
				 if(groupByChinaAndStatusMap.containsKey(content + "---" + excelLanguageStatus)) {
					 content =  content.replaceAll(content, groupByChinaAndStatusMap.get(content + "---" + excelLanguageStatus).get(0).getOtherStr());
				}
				 sb.append(content);
			}
			 sb.append(";");
		}
		 
		 return sb.toString();
	}
	
	
	public static Map<String, List<ExcelLanguageDTO>> getMapByClassName(List<List<String>> errMsgList) {
		String className = EasyExcelAspect.threadLocal.get();
		if(StringUtils.isEmpty(className)) {
			return new HashMap<>();
		}
		List<ExcelLanguageDTO> list = realHandelerMap.get(className);
		if(list == null || list.size() <= 0) {
			return new HashMap<>();
		}
		 Map<String, List<ExcelLanguageDTO>> groupByChinaAndStatusMap = list.stream().collect(Collectors.groupingBy(p -> p.getChinaStr() + "---" + p.getLanguageStatus()));
		return groupByChinaAndStatusMap;
	}
	
	private static String toString(List<List<String>> errMsgList) {
		StringBuilder errMsgStr = new StringBuilder();
		for (List<String> string : errMsgList) {
			for (String content : string) {
				errMsgStr.append(content);
			}
			errMsgStr.append(";");
		}
		return errMsgStr.toString();
	}
}
